package com.google.project.client.ProjectManager;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.project.client.Service.ProjectService;
import com.google.project.client.Service.ProjectServiceAsync;
import com.google.project.shared.ProjectDTO;

public class PMUpdateProjectInfo extends Composite {

	private static final Binder binder = GWT.create(Binder.class);
	@UiField Button btnUpdateProject;
	@UiField TextBox txtProjectCode;
	@UiField TextBox txtProjectName;
	@UiField TextBox txtLocation;
	@UiField TextBox txtConsultants;
	@UiField TextBox txtPhoneConsultants;
	@UiField TextBox txtFaxConsultant;
	@UiField TextBox txtInvestor;
	@UiField TextBox txtPhoneInvestor;
	@UiField TextBox txtFaxInvestor;
	@UiField TextArea txtDescription;
	@UiField Button btnCancel;
	@UiField DateBox dbStartDate;
	@UiField DateBox dbDueDate;

	interface Binder extends UiBinder<Widget, PMUpdateProjectInfo> {
	}

	public PMUpdateProjectInfo() {
		initWidget(binder.createAndBindUi(this));
		dbStartDate.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat("dd/MM/yyyy")));
		dbDueDate.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat("dd/MM/yyyy")));
	}
	private ProjectDTO currentProject = new ProjectDTO();

	private final ProjectServiceAsync admService = GWT.create(ProjectService.class);
	
	/***
	 * contructor update project info
	 */
	public PMUpdateProjectInfo(String projectId) {
		this();
		getProjectInfo(projectId);
	}
  
	
	private void displayProject() {
		txtProjectCode.setText(currentProject.getProjectCode());
		txtProjectName.setText(currentProject.getProjectName());
		dbStartDate.setValue(new Date(currentProject.getStartDate()));
		dbDueDate.setValue( new Date(currentProject.getDueDate()));
		txtLocation.setValue(currentProject.getLocation());
		txtConsultants.setText(currentProject.getConsultant());
		txtFaxConsultant.setText(currentProject.getFaxConsultant());
		txtPhoneConsultants.setText(currentProject.getPhoneConsultant());
		txtInvestor.setText(currentProject.getInvestor());
		txtPhoneInvestor.setText(currentProject.getPhoneInvestor());
		txtFaxInvestor.setText(currentProject.getFaxInvestor());
		txtDescription.setText(currentProject.getDescription());
	}

	private void getProjectInfo(String projectId) {
		admService.getProjectById(projectId, new AsyncCallback<ProjectDTO>() {
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Edit fail!");

			}

			@Override
			public void onSuccess(ProjectDTO result) {
				currentProject = result;
				displayProject();
			}

		});

	}
	//get value from form
	private void getFormValues() {
		currentProject.setProjectCode(txtProjectCode.getText());
		currentProject.setProjectName(txtProjectName.getText());
		
		if (dbStartDate.getValue() != null) {
		
			currentProject.setStartDate(dbStartDate.getValue().getTime());
		}
		if (dbDueDate.getValue() != null) {
			currentProject.setDueDate(dbDueDate.getValue().getTime());
		}
		currentProject.setLocation(txtLocation.getText());
		currentProject.setConsultant(txtConsultants.getText());
		currentProject.setFaxConsultant(txtFaxConsultant.getText());
		currentProject.setPhoneConsultant(txtPhoneConsultants.getText());
		currentProject.setInvestor(txtInvestor.getText());
		currentProject.setPhoneInvestor(txtPhoneInvestor.getText());
		currentProject.setFaxInvestor(txtFaxInvestor.getText());
		currentProject.setDescription(txtDescription.getText());
	}
	@UiHandler("btnUpdateProject")
	void onBtnUpdateProjectClick(ClickEvent event) {
		getFormValues();
		admService.updateProject(currentProject, new AsyncCallback<Void>() {

			@Override
			public void onSuccess(Void result) {
				Window.alert("Cập nhật thành công!");
				PMEntrypoint.get().PMProjectDetail(currentProject.getProjectID());
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("An error occurred");

			}
		});
	}
	@UiHandler("btnCancel")
	void onBtnCancelClick(ClickEvent event) {
		PMEntrypoint.get().PMProjectDetail(currentProject.getProjectID());
	}
}
